package net.cyndeline.fov

/**
 * Uses a two-dimensional matrix to store map visibility results in.
 */
class MatrixFieldOfView extends FieldOfViewResult {
  private var visibilityMap: Array[Array[VisibilityStatus]] = Array.fill[VisibilityStatus](0, 0) { Hidden }
  
  def create(width: Int, height: Int) {
    visibilityMap = Array.fill[VisibilityStatus](width, height) { Hidden }
  }
  
  override def visibility(x: Int, y: Int): Option[VisibilityStatus] = {
    if (positionIsValid(x, y)) {
      Option(visibilityMap(x)(y))
    } else {
      None
    }
  }
  
  override def setVisibility(x: Int, y: Int, status: VisibilityStatus) {
    if (positionIsValid(x, y)) {
      visibilityMap(x)(y) = status
    }
  }
  
  private def positionIsValid(x: Int, y: Int): Boolean = {
    (x < visibilityMap.length && x >= 0 && y < visibilityMap(0).length && y >= 0)
  }

}